package edu.yngsxy.wkstudy.core.codegen;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.*;

@Data
@Schema(description = "代码生成器配置")
public class CodeGeneratorConfig {
    // 数据库配置
    private String url = "jdbc:mysql://localhost:3306/deepseeksdudy?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
    private  String username = "root";// 用户名
    private  String password = "123456";// 密码
    // 包名配置
    @Schema(description = "项目父包名", example = "com.example.demo")
    private  String parentPackage = "org.example.demo2";// 父包名
    @Schema(description = "模块名", example = "temp")
    private  String moduleName = "temp";    // 模块名
    @Schema(description = "entity实体层包名", example = "model.entity")
    private  String entityPackage = "model.entity";
    @Schema(description = "mapper数据访问层包名", example = "dao")
    private  String mapperPackage = "dao";
    @Schema(description = "service业务逻辑层包名", example = "service")
    private  String servicePackage = "service";
    @Schema(description = "serviceImpl业务逻辑层实现包名", example = "service.impl")
    private  String serviceImplPackage = "service.impl";
    @Schema(description = "Controller控制器包名", example = "controller.admin")
    private  String controllerPackage = "controller.admin";    // 设置 Controller 包名
    @Schema(description = "Controller控制器路径前缀", example = "api")
    private  String controllerPathPrefix = "api";    // 控制器路径前缀
    @Schema(description = "mapperXml文件存放目录名称，以/src/main/resources/为根目录", example = "mapper")
    private  String mapperXmlDir = "mapper";
    @Schema(description = "项目名称", example = "悟空学堂",requiredProperties = "true")
    private  String projectName = "悟空学堂"; // 项目名称
    @Schema(description = "项目路径",title = "项目路径，默认自动获取当前项目路径")
    private  String projectPath = System.getProperty("user.dir");// 项目路径
    @Schema(description = "作者名",example = "qyc")
    private  String author = "qyc"; // 作者名
    @Schema(description = "生成表名,逗号分隔的字符串,如user,order",example = "")
    private  String tables = null; // 要生成的表名,逗号分隔的字符串（如 "user,order"）,默认所有
    @Schema(description = "生成表名前缀,根据表前缀生成代码，如sys则生成sys开头的表对应的代码",example = "sys")
    private  String tablePrefix = null; //根据表前缀生成代码，如sys则生成sys开头的表对应的代码
    @Schema(description = "是否启用生成通用查询结果集",example = "true")
    private  boolean enableBaseResultMap = true; // 是否启用通用查询结果集
    @Schema(description = "是否启用生成通用查询结果列名",example = "false")
    private  boolean enableBaseColumnList = false; // 是否启用通用查询结果列名
    @Schema(description = "过滤表前缀,生成的实体类名将会去除前缀")
    private List<String> tablePrefixDeleted =new ArrayList<>();//设置过滤表前缀,生成的实体类名将会去除前缀
    // 定义需要注入的自定义变量
    @Schema(description = "注入模板变量")
    private Map<String, Object> customMap = new HashMap<>();

    public static CodeGeneratorConfig create() {//默认配置
        return create(null, null, null);
    }
    public static CodeGeneratorConfig create(String url, String username, String password) {
        CodeGeneratorConfig config = new CodeGeneratorConfig(url, username, password);
        return config;
    }
    public CodeGeneratorConfig(){}
    public CodeGeneratorConfig(String url, String username, String password){
        Optional.ofNullable(url).ifPresent(val -> this.url = val);
        Optional.ofNullable(username).ifPresent(val -> this.username = val);
        Optional.ofNullable(password).ifPresent(val -> this.password = val);
    }
    public void  init(){
        // 空值初始化简化
        this.tables = Objects.requireNonNullElse(this.tables, "");
        this.tablePrefixDeleted = Objects.requireNonNullElse(this.tablePrefixDeleted, new ArrayList<>());
        this.customMap = Objects.requireNonNullElse(this.customMap, new HashMap<>());
        // 直接使用当前对象的customMap，避免重复定义变量
        this.customMap.put("projectName", this.getProjectName());
        this.customMap.put("controllerPathPrefix", this.getControllerPathPrefix());
    }
}
